#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){
	// listen on the given port
	cout << "listening for osc messages on port " << PORT << "\n";
	receiver.setup( PORT );

	ofSetFrameRate(120);
	
	camWidth 		= 1024;	// try to grab at this size. 
	camHeight 		= 800;
	
	vidGrabber.setVerbose(true);
	vidGrabber.initGrabber(camWidth,camHeight);

	ofSoundStreamSetup(0,2,this, 44100, BUFF, 4);	
	left = new float[BUFF];
	right = new float[BUFF];

	bufferCounter = 0;
	drawCounter = 0;
	Nplayer = 1;
	size = 2000;

	for (int i = 0; i < MAX_PLAYERS; i++)
	   pos_player[i].set(0,0);
	for (int i = 0; i < MAX_PLAYERS; i++)
	   count[i]=0;

}

//--------------------------------------------------------------
void testApp::update(){

	vidGrabber.grabFrame();

	// check for waiting messages
	while( receiver.hasWaitingMessages() )
	{
		// get the next message
		ofxOscMessage m;
		receiver.getNextMessage( &m );

		// check for mouse moved message
		if ( m.getAddress() == "user" )
		{

		    int OSCparam = m.getArgAsInt32( 0 );
		    string OSCparam1 = m.getArgAsString( 1 );
		    int OSCparam2 = m.getArgAsInt32( 2 );
		    if (OSCparam==0)
		    {
//                if (OSCparam1 == "play" && OSCparam2==1)
//                    Nplayer = 0;
                if (OSCparam1 == "size")
                    size = OSCparam2;
		    }
		    if (OSCparam==1)
		    {
                if (OSCparam1 == "play" && OSCparam2==1)
                    Nplayer = 1;
		    }
		    if (OSCparam==2)
		    {
                if (OSCparam1 == "play" && OSCparam2==1)
                    Nplayer = 2;
		    }
		    if (OSCparam==3)
		    {
                if (OSCparam1 == "play" && OSCparam2==1)
                    Nplayer = 3;
		    }
		    if (OSCparam==4)
		    {
                if (OSCparam1 == "play" && OSCparam2==1)
                    Nplayer = 4;
		    }
		    if (OSCparam==5)
		    {
                if (OSCparam1 == "play" && OSCparam2==1)
                    Nplayer = 5;
		    }
		    if (OSCparam==6)
		    {
                if (OSCparam1 == "play" && OSCparam2==1)
                    Nplayer = 6;
		    }
		    if (OSCparam==7)
		    {
                if (OSCparam1 == "play" && OSCparam2==1)
                    Nplayer = 7;
		    }
		    if (OSCparam==8)
		    {
                if (OSCparam1 == "play" && OSCparam2==1)
                    Nplayer = 8;
		    }
		    if (OSCparam==9)
		    {
                if (OSCparam1 == "play" && OSCparam2==1)
                    Nplayer = 9;
		    }
		    if (OSCparam==10)
		    {
                if (OSCparam1 == "play" && OSCparam2==1)
                    Nplayer = 10;
		    }
		    if (OSCparam==11)
		    {
                if (OSCparam1 == "play" && OSCparam2==1)
                    Nplayer = 11;
		    }
		    if (OSCparam==12)
		    {
                if (OSCparam1 == "play" && OSCparam2==1)
                    Nplayer = 12;
		    }
		}
	}
}


//--------------------------------------------------------------
void testApp::draw(){
	ofSetColor(0xffffff);
	vidGrabber.draw(0,0);

	if (size > 50)
		count[Nplayer-1] = 0;
	if (size < 50 && size > 25)
		count[Nplayer-1] = 2;
	if (size < 26)
		count[Nplayer-1] = 12;


	for (int j = 0; j < MAX_PLAYERS; j++)
	{
		if(Nplayer==j+1 || count[j]>0)
		{
			count[j]--;
			prop = (pos_player[j].y-20)/(float)BUFF;

			ofEnableAlphaBlending();
			for (int i = 0; i < BUFF; i++){
				ofSetColor(255,0,0,65);
				ofSetLineWidth(4);
				ofLine(pos_player[j].x,(int)(i*prop),pos_player[j].x+left[i]*150.0f,(int)(i*prop));
				ofSetColor(255,255,0,255);
				ofCircle(pos_player[j].x+left[i]*150.0f,(int)(i*prop),2);
			}	

		}
	}

	for (int i = 0; i < MAX_PLAYERS; i++)
	{
		string info = ofToString(i+1);
		ofEnableAlphaBlending();
		ofSetColor(255, 255, 255, 70);
		ofDrawBitmapString(info, pos_player[i].x-15, pos_player[i].y-15);
	}

}

static float adder = 0;
//--------------------------------------------------------------
void testApp::audioReceived 	(float * input, int bufferSize, int nChannels){	
	// samples are "interleaved"
	for (int i = 0; i < bufferSize; i++){
		left[i] = input[i*2];
		right[i] = input[i*2+1];
	}
	bufferCounter++;
	
}

//--------------------------------------------------------------
void testApp::keyPressed  (int key){
   if(key=='1')
	   pos_player[0].set(mouseX,mouseY);
   if(key=='2')
	   pos_player[1].set(mouseX,mouseY);
   if(key=='3')
	   pos_player[2].set(mouseX,mouseY);
   if(key=='4')
	   pos_player[3].set(mouseX,mouseY);
   if(key=='5')
	   pos_player[4].set(mouseX,mouseY);
   if(key=='6')
	   pos_player[5].set(mouseX,mouseY);
   if(key=='7')
	   pos_player[6].set(mouseX,mouseY);
   if(key=='8')
	   pos_player[7].set(mouseX,mouseY);
   if(key=='9')
	   pos_player[8].set(mouseX,mouseY);
   if(key=='0')
	   pos_player[9].set(mouseX,mouseY);
   if(key=='o')
	   pos_player[10].set(mouseX,mouseY);
   if(key=='p')
	   pos_player[11].set(mouseX,mouseY);

   if(key=='q')
   {
	   Nplayer++;
	   if (Nplayer>12)
		   Nplayer=1;
   }
   if(key=='a')
   {
	   Nplayer--;
	   if (Nplayer<0)
		   Nplayer=12;
   }
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

